
package com.lbb.ftp;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FTPUtil
{
  private static String hostName = "127.0.0.1";

  private static String userName = "ftpuser";

  private static String password = "ts123456";

  private FTPClient ftpClient = null;

  public FTPUtil(String hostName, String userName, String password) 
  {
    try
    {
      ftpClient = new FTPClient();
      ftpClient.connect(hostName);
      ftpClient.login(userName, password);
      ftpClient.setControlEncoding("GBK");
      ftpClient.setFileType(2);//设置文件类型为2禁止    否则默认传送的文件字节数会变多
		
    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("初始化FTP连接发生异常！", e);
    }
  }

  public  void closeFtp()
  {
    try
    {
      ftpClient.logout();
      ftpClient.disconnect();
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("关闭FTP连接发生异常！", e);
    }
  }

  public  void download(String remoteFileName, String localFileName)
  {
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(localFileName);
      download(remoteFileName, fos);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
  
  public  void downloadinput(String remoteFileName, String localFileName)
  {
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(localFileName);
      //downloadinput(remoteFileName, fos);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
  
  
  public void download(String remoteFileName, OutputStream fos)
  {
    try
    {
      if ((remoteFileName != null) && (!"".equals(remoteFileName)) && (fos != null)) {
        ftpClient.setBufferSize(1024);

        ftpClient.setFileType(2);
      boolean flag =  ftpClient.retrieveFile(remoteFileName, fos);
      
      System.out.println(flag);
      } else {
        System.out.println("参数不合法");
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
//      IOUtils.closeQuietly(fos);
    	
    	try {
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
      closeFtp();
    }
  }

  public String upload(String workingDirectory, String fileName, String localFileName)
  {
    FileInputStream fis = null;
    File srcFile = new File(localFileName);
    try {
      fis = new FileInputStream(srcFile);
      fileName = upload(workingDirectory, fileName, fis);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    return fileName;
  }

  public String upload(String workingDirectory, String fileName, InputStream fis)
  {
    boolean b = false;

    label176: 
    try { if ((workingDirectory != null) && (!"".equals(workingDirectory)) && 
        (fileName != null) && (!"".equals(fileName)) && 
        (fis != null)) {
//        fileName = DateUtils.getFormatDate("yyyyMMddHHmmssSSS") + "_" + fileName;

        if (workingDirectory.lastIndexOf("/") == workingDirectory.length()) {
          workingDirectory = workingDirectory.substring(0, workingDirectory.length() - 1);
        }
        ftpClient.changeWorkingDirectory(workingDirectory);
        ftpClient.setBufferSize(1024);
        ftpClient.setControlEncoding("GBK");

        ftpClient.setFileType(2);
        b = ftpClient.storeFile(fileName, fis);
        if (!b) {
          fileName = null;

          break label176;
        } } else { System.out.println("参数不合法"); }
    } catch (IOException e)
    {
      e.printStackTrace();
    } finally {
      
      try {
		fis.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
      closeFtp();
    }
    return fileName;
  }

  public boolean deleteFile(String remoteFileName)
  {
    boolean b = false;
    try {
      b = ftpClient.deleteFile(remoteFileName);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      closeFtp();
    }
    return b;
  }

  public FTPClient getFtpClient()
  {
    return ftpClient;
  }

  public void setFtpClient(FTPClient ftpClient) {
    ftpClient = ftpClient;
  }

  public static void main(String[] args) throws FileNotFoundException
  {
    FTPUtil f = new FTPUtil(hostName, userName, password);
    
    FileInputStream in;
			
	//		
	//		
	//		OutputStream out = new  FileOutputStream("d:\\2.jpg");
	//		f.download("20190123113345628_fcj111111.jpg", out);
	//		
    
    
   String fileName = "qq2018130-625.rar";
   
   final String ftpRoot = "ss/";
   
   final String localRoot = "d:\\";
			try {
				FTPFile[] files =  f.ftpClient.listFiles(ftpRoot+fileName);
				
				
				//已有 断点 续传
				if (files != null && files.length >0){
					
					
					FTPFile file = files[0];
					
					long seek = file.getSize();
					
					System.out.println("seek 字节数:"+ seek);
					f.randomAccessFile(seek,fileName,ftpRoot,localRoot);
				}
				
				else {
					
					in = new FileInputStream(new File(localRoot+fileName));
					String  s = f.upload("/ss", fileName, in);
				}
				
				
				System.out.println("-------------处理结束-------------");
				
			} catch (IOException e) {
				e.printStackTrace();
			}
//			System.out.println(s);
    
  }
  
  
  
  
      public void  randomAccessFile(long seek,String fileName,String ftpRoot,String localRoot){
    	  
    	  
    	  RandomAccessFile raf = null;
    	  
    	  ByteArrayInputStream bais = null;
		try {
			raf = new RandomAccessFile(localRoot+fileName,"r");
			raf.seek(seek);// 读取时，将指针重置到文件的开始位置。
			
			byte[] buff = new byte[1024*1024*100];
			
			int len;
			
			// 使用 ftp appe 命令像文件末尾追加数据
			OutputStream out = ftpClient.appendFileStream(ftpRoot+fileName);
			
//			bais = new ByteArrayInputStream (buff);
			while ( (len = raf.read(buff)) != -1){
//				bais.read(buff,0,len);
				System.out.println("读取字节数:"+len);
//				ftpClient.appendFile(ftpRoot+fileName, new ByteArrayInputStream (buff,0,len));
				
				out.write(buff, 0, len);
				
			}
			
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			
			try {
				raf.close();
//				bais.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    	  
      }
}